import path from 'path'
import Store from '../store/index'

/**
 * 配置
 * @type {{get(): *, set(*=): void}}
 */
export const config = {
  get(name) {
    let config = localStorage.getItem(name)
    if (!config) {
      switch (name) {
        // 软件默认配置
        case 'softconfig':
          return {
            face_dat_path: path.join(Store.state.__root, '/data/face_dat/'),
            face_img_path: path.join(Store.state.__root, '/data/face_img/'),
            data_ptah: path.join(Store.state.__root, '/data/'),
            threshold: 60,
            interval: 5
          }
        // 虹软SDK
        case 'aiconfig':
          return {
            AppID: '',
            FT_key: '',
            FD_key: '',
            FR_key: ''
          }
      }
    } else {
      config = JSON.parse(config)
      return config || false
    }
  },
  set(name, val) {
    if (val) {
      val = typeof (val) === 'object' ? JSON.stringify(val) : val
      localStorage.setItem(name, val)
    }
  }
}

/**
 * 对象深度拷贝
 * @param source
 * @returns {Array|{}}
 */
export function deepCopy(source) {
  let result
  if (source.constructor === Array) {
    result = []
    for (const key in source) {
      result[key] = typeof source[key] === 'object' ? deepCopy(source[key]) : source[key]
    }
  } else {
    result = {}
    for (const jsonkey in source) { // eslint-disable-line
      result[jsonkey] = typeof source[jsonkey] === 'object' ? deepCopy(source[jsonkey]) : source[jsonkey]
    }
  }
  return result
}
